package com.sky.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Collection;
import java.util.concurrent.TimeUnit;

@Slf4j
@Component
public class RedisCacheUtils {
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    /**
     * 缓存基本对象 String，Integer
     */
    public void setCacheObject(String key,Object value){
        redisTemplate.opsForValue().set(key,value);
    }

    public void setCacheObject(String key, Object value, Long time, TimeUnit timeUnit){
        redisTemplate.opsForValue().set(key,value,time,timeUnit);
    }

    public Long getExpire(String key){
        return redisTemplate.getExpire(key);
    }

    public Boolean hasKey(String key){
        return redisTemplate.hasKey(key);
    }

    public Object getCacheObject(String key){
        return redisTemplate.opsForValue().get(key);
    }

    public Boolean delete(String key){
        return redisTemplate.delete(key);
    }

    public Collection<String> keys(String pattern){
        return redisTemplate.keys(pattern);
    }

}
